package com.zx.znav.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.znav.bean.Link;
import com.zx.znav.service.LinkService;
import com.zx.znav.mapper.LinkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author lenovo
* @description 针对表【t_link】的数据库操作Service实现
* @createDate 2023-07-25 14:32:40
*/
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link>
        implements LinkService{
    @Autowired
    private LinkMapper linkMapper;

    @Override
    public List<Link> listLink() {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Link::getFirstNavId).orderByAsc(Link::getSecondNavId);
        wrapper.orderByAsc(Link::getTitle);
        return list(wrapper);
    }


    @Override
    public Page<Link> pageLink(Link link, Integer pageNo, Integer pageSize) {
        Page<Link> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(link.getFirstNavId() != null, Link::getFirstNavId, link.getFirstNavId());
        wrapper.eq(link.getSecondNavId() != null, Link::getSecondNavId, link.getSecondNavId());
        wrapper.like(link.getTitle() != null, Link::getTitle, link.getTitle());
        wrapper.orderByAsc(Link::getFirstNavId).orderByAsc(Link::getSecondNavId);
        wrapper.orderByDesc(Link::getPriority);
        wrapper.orderByAsc(Link::getTitle);
        return page(page, wrapper);
    }

    @Override
    public List<Link> listLinkByFirsNavIdAndSecondNavId(Integer firstNavId, Integer secondNavId) {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Link::getFirstNavId, firstNavId);
        wrapper.eq(Link::getSecondNavId, secondNavId);
        wrapper.orderByDesc(Link::getPriority);
        wrapper.orderByAsc(Link::getTitle);
        return list(wrapper);
    }

    @Override
    public Integer countLinkByByFirsNavIdAndSecondNavId(Integer firstNavId, Integer secondNavId) {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Link::getFirstNavId, firstNavId);
        wrapper.eq(Link::getSecondNavId, secondNavId);
        return count(wrapper);
    }

    @Override
    public List<Link> listNLinkBySecondNavId(Integer secondNavId, Integer size) {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Link::getSecondNavId, secondNavId);
        wrapper.orderByDesc(Link::getViewCnt).orderByAsc(Link::getTitle);
        wrapper.last("limit " + size);
        return list(wrapper);
    }

    @Override
    public List<Link> listByIds(List<Integer> ids) {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Link::getId, ids);
        wrapper.orderByDesc(Link::getViewCnt).orderByAsc(Link::getTitle);
        return list(wrapper);
    }

    @Override
    public List<Link> search(String input) {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Link::getTitle, input).or().like(Link::getBrief, input);
        wrapper.orderByDesc(Link::getViewCnt).orderByAsc(Link::getTitle);
        return list(wrapper);
    }

    @Override
    public List<Map<String, Integer>> groupByFirstNav() {
        return linkMapper.groupByFirstNav();
    }

    @Override
    public void increaseViewCnt(Integer id) {
        linkMapper.incrViewCnt(id);
    }

    @Override
    public List<Link> topLink(Integer n) {
        LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Link::getViewCnt);
        wrapper.last("limit " + n);
        return list(wrapper);
    }

}




